package com.mysql2oracle;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.*;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

/**
 * Hello world!
 *
 */
public class Utils
{
    public static String mysql_jdbc_url;
    public static String user;
    public static String password;

    public static String jdbc_url;
    public static String jdbc_user;
    public static String jdbc_password;

    public static List<String> tableNames;

    public void startProcess(String tableName) throws Exception {
        // 创建到两个数据库的连接
        Class.forName("org.gjt.mm.mysql.Driver");
        Connection connSource = DriverManager.getConnection(mysql_jdbc_url, user, password);

        Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection connDest = DriverManager.getConnection(jdbc_url, jdbc_user, jdbc_password);

        // 打开源数据库中相关表
        StringBuilder sb = new StringBuilder();
        // 主要是tag_category的mysql表字段中有oracle字段的关键字，特殊处理
        if(!"tag_category".equals(tableName)) {
            sb.append("insert into " + tableName + "(");
        } else {
            sb.append("insert into " + tableName);
        }
        Statement stmt = connSource.createStatement();
        ResultSet rs = stmt.executeQuery("select * from " + tableName);

        // 显示共计有多少条记录
        rs.last();
        System.out.println(tableName + "表共计有：" + rs.getRow() + "条记录，正在处理......");
        rs.beforeFirst();

        // 先计算目标数据库的PreparedStatement的SQL语句
        ResultSetMetaData rsmd = rs.getMetaData();
        int numberOfColumns = rsmd.getColumnCount();
        // 主要是tag_category的mysql表字段中有oracle字段的关键字，特殊处理
        if(!"tag_category".equals(tableName)) {
            for (int i = 1; i <= numberOfColumns; i++) {
                sb.append(rsmd.getColumnName(i) + ",");
            }
            sb.deleteCharAt(sb.length() - 1);
            sb.append(")values(");
        } else {
            sb.append(" values(");
        }
        for (int i = 1; i <= numberOfColumns; i++) {
            sb.append("?,");
        }
        sb.deleteCharAt(sb.length() - 1);
        sb.append(")");
        System.out.println(sb.toString());

        // 给PreparedStatement赋值，然后更新；如果是大数量的情况，可以考虑Batch处理。因为这里的数据量小，直接单条更新了。
        PreparedStatement pstmt = connDest.prepareStatement(sb.toString());
        while (rs.next()) {
            for (int i = 1; i <= numberOfColumns; i++) {
                pstmt.setObject(i, rs.getObject(i));
            }

            pstmt.executeUpdate();
        }
        System.out.print("---更新完成---");

        // 关闭各资源
        rs.close();
        stmt.close();
        pstmt.close();

        connSource.close();
        connDest.close();
    }

    public static void main( String[] args ) throws Exception {
        if(args.length != 1) {
            System.out.println("参数不正确");
            return;
        }
        Utils.loadProperties(args[0]);
        Utils ins = new Utils();

        for(String tableName : tableNames) {
            ins.startProcess(tableName.trim());
        }

    }

    public static void loadProperties(String profile) {
        System.out.println("开始load properties.....");
        InputStream is = Utils.class.getClassLoader()
                .getResourceAsStream("jdbc-" + profile + ".properties");
        Properties properties = new Properties();
        try {
            properties.load(new InputStreamReader(is));
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("读取properties profile配置成功: "
                + properties.getProperty("profile"));
        mysql_jdbc_url = properties.getProperty("mysql.url");
        user = properties.getProperty("mysql.user");
        password = properties.getProperty("mysql.pwd");
        jdbc_url = properties.getProperty("oracle.url");
        jdbc_user = properties.getProperty("oracle.user");
        jdbc_password = properties.getProperty("oracle.pwd");

        tableNames = Arrays.asList(properties.getProperty("tables").split(","));

        System.out.println("结束load properties.....");
    }
}
